<?php

namespace app\admin\controller;

use think\exception\ValidateException;
use app\model\DiyPage;
use think\db\Where;

class DiypageController extends Base
{

	function index()
	{
		$weid = weid();
		$keyword = input('post.keyword', '', 'serach_in');
		$query = DiyPage::where(['weid' => $weid]);
		if (!empty($keyword)) {
			$query->where('title', 'like', '%' . $keyword . '%');
		}

		$res = $query->order('id asc')
			->paginate(getpage())
			->toArray();

		foreach ($res['data'] as &$vo) {
			$vo['create_time'] = time_format($vo['create_time']);
			$vo['update_time'] = time_format($vo['update_time']);
		}

		$data['data'] = $res;
		return $this->json($data);
	}
	public function add()
	{
		$weid = weid();
		$data['weid'] = $weid;
		$data['title'] = '自定义页面';
		$data['version'] = 2;
		$data['create_time'] = time();
		$data['pagebase'] = serialize(input('post.pagebase'));
		$res = DiyPage::create($data);
		return $this->json(['msg' => '添加成功', 'data' => $res->id]);
	}

	public function update()
	{
		$id = $this->request->post('id');
		$weid = weid();
		$postdata = input('post.');
		$data['update_time'] = time();

		if (!empty($postdata['pageList'])) {
			foreach ($postdata['pageList'] as $vo) {

				$dp	= DiyPage::where(['weid' => $weid, 'id' => $vo['id'], 'version' => 2])->find();
				$data['weid'] = $weid;
				$data['title'] = $vo['pagebase'][0]['params']['title'];
				$data['pagebase'] = serialize($vo['pagebase']);
				$data['modulelist'] = serialize($vo['modulelist']);
				$data['create_time'] = time(); 
				$data['update_time'] = time();
				$data['version'] = 2;
				$data['status'] = 1;
				if (empty($dp)) {
					try {
						$res = DiyPage::create($data);
					} catch (\Exception $e) {
						throw new ValidateException($e->getMessage());
					}
				} else {
					$data['id'] = $vo['id'];
					try {
						DiyPage::update($data);
					} catch (\Exception $e) {
						throw new ValidateException($e->getMessage());
					}
				}
			}
			return $this->json(['msg' => '保存成功']);
		} else {
			if (empty($id)) {
				$data['weid'] = $weid;
				$data['title'] = $postdata['title'];
				$data['is_index'] = $postdata['is_index'];
				$data['status'] = $postdata['status'];
				$data['create_time'] = time();
				try {
					$res = DiyPage::create($data);
				} catch (\Exception $e) {
					throw new ValidateException($e->getMessage());
				}

				return $this->json(['msg' => '添加成功', 'data' => $res->id]);
			} else {

				try {
					DiyPage::update($data);
				} catch (\Exception $e) {
					throw new ValidateException($e->getMessage());
				}
				return $this->json(['msg' => '修改成功']);
			}
		}
	}

	public function listUpdate()
	{
		$data = only('id,status,is_audit,is_index');
		if (!$data['id']) throw new ValidateException('参数错误');
		DiyPage::update($data);

		return $this->json(['msg' => '操作成功']);
	}

	public function setaudit()
	{

		$id = input('post.id');
		DiyPage::where('id', '>', 0)->where(['version' => 2])->update(['is_audit' => 0]);
		DiyPage::where('id', '=', $id)->update(['is_audit' => 1]);

		return $this->json(['msg' => '设为审核成功']);
	}

	public function setindex()
	{

		$id = input('post.id');
		DiyPage::where('id', '>', 0)->where(['version' => 2])->update(['is_index' => 0]);
		DiyPage::where('id', '=', $id)->update(['is_index' => 1]);

		return $this->json(['msg' => '设置首页成功']);
	}

	function getInfo()
	{

		$id =  $this->request->post('id', '', 'serach_in');
		if (!$id) throw new ValidateException('参数错误');
		$data = DiyPage::field('*')->find($id)->toArray();

		$data['url'] =	'https://' . $_SERVER['SERVER_NAME'] . '/addons/' . config('my.app_name')  . '/core/web/index.php?do=admin/diypage/update&id=' . $id . '&i=' . weid();

		return $this->json(['data' => $data]);
	}
	function getpageInfo()
	{

		$data = DiyPage::where(['weid' => weid(), 'version' => 2])->select()->toArray();
		if (!empty($data)) {
			foreach ($data as &$vo) {
				$vo['title'] = $vo['title'];
				$vo['pagebase'] = iunserializer($vo['pagebase']);
				$vo['modulelist'] = iunserializer($vo['modulelist']);
			}
		} else {
			$data = '';
		}

		return $this->json(['data' => $data]);
	}

	function delete()
	{
		$idx =  $this->request->post('id', '', 'serach_in');
		if (!$idx) throw new ValidateException('参数错误');
		DiyPage::destroy(['id' => explode(',', $idx)], true);
		return $this->json(['msg' => '操作成功']);
	}
}
